package com.jiangyg.mall.coupon.controller;

import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.core.support.validation.Business1Group;
import com.jiangyg.mall.coupon.dto.HomeAdvDTO;
import com.jiangyg.mall.coupon.service.HomeAdvService;
import com.jiangyg.mall.coupon.vo.HomeAdvVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：首页轮播广告控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-18 21:33:06
 */
@RestController
@RequestMapping("/coupon/home-adv")
public class HomeAdvController {

    /**
     * 首页轮播广告接口
     */
    private final HomeAdvService homeAdvService;

    @Autowired
    public HomeAdvController(HomeAdvService homeAdvService) {
        this.homeAdvService = homeAdvService;
    }

    /**
     * 功能描述：首页轮播广告分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页首页轮播广告
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<HomeAdvVO>> select(HttpServletRequest request, @RequestBody Pagination<HomeAdvVO> pagination) throws Exception {
        return Result.ok(homeAdvService.selectPagination(pagination));
    }

    /**
     * 功能描述：保存或者修改首页轮播广告
     *
     * @param request 请求
     * @param dto     首页轮播广告
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated HomeAdvDTO dto) throws Exception {
        return Result.saveOrUpdate(homeAdvService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取首页轮播广告
     *
     * @param request   请求
     * @param homeAdvId 首页轮播广告ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<HomeAdvVO> info(HttpServletRequest request, @RequestParam(name = "homeAdvId") Long homeAdvId) throws Exception {
        return Result.ok(homeAdvService.selectById(homeAdvId));
    }

    /**
     * 功能描述：删除首页轮播广告
     *
     * @param request    请求
     * @param homeAdvIds 批量删除的首页轮播广告ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> homeAdvIds) {
        return Result.delete(homeAdvService.delete(homeAdvIds));
    }

    /**
     * 功能描述：切换广告状态事件
     *
     * @param request 请求
     * @param dto     参数
     * @return 返回分页首页轮播广告
     */
    @ResponseBody
    @PostMapping("/switch-status")
    public Result<Object> switchStatus(HttpServletRequest request,
                                       @RequestBody @Validated(Business1Group.class) HomeAdvDTO dto) throws Exception {
        return Result.saveOrUpdate(homeAdvService.switchStatus(dto));
    }

    /**
     * 功能描述：加载首页广告缓存
     *
     * @param request 请求
     * @return 返回分页首页轮播广告
     */
    @ResponseBody
    @PostMapping("/load-cache")
    public Result<Object> loadCache(HttpServletRequest request) throws Exception {
        // TODO 追加分布式定时任务
        homeAdvService.loadCache();
        return Result.ok();
    }

    /**
     * 功能描述：查询简化的首页广告（一般用于商城显示）
     *
     * @param request 请求
     * @return 返回分页首页轮播广告
     */
    @ResponseBody
    @GetMapping("/c-list")
    public String selectListByCache(HttpServletRequest request) throws Exception {
        final String result = homeAdvService.selectListByCache();
        if (StringUtils.isBlank(result)) {
            return Result.error().toJSONString();
        }
        return result;
    }

    /**
     * 功能描述：记录首页广告点击次数
     *
     * @param request   请求
     * @param homeAdvId 首页轮播广告ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/click")
    public Result<Object> click(HttpServletRequest request, @RequestParam(name = "homeAdvId") Long homeAdvId) {
        homeAdvService.click(homeAdvId);
        return Result.ok();
    }

}
